package com.tcat.item.web;

import com.tcat.common.vo.PageResult;
import com.tcat.item.pojo.Brand;
import com.tcat.item.service.BrandService;
import com.tcat.item.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "品牌管理")
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 分页查询品牌
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param search
     * @return
     */
    @ApiOperation(value = "分页查询品牌")
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value="page",defaultValue = "1") Integer page,
            @RequestParam(value="rows",defaultValue = "5") Integer rows,
            @RequestParam(value="sortBy",required = false) String sortBy,
            @RequestParam(value="desc",defaultValue = "false") Boolean desc,
            @RequestParam(value="search",required = false) String  search
    ){
         return ResponseEntity.ok(brandService.queryBrandByPage(page,rows,sortBy,desc,search));
    }

    /**
     * 新增品牌
     * @param brand
     * @param cids
     * @return
     */
    @ApiOperation(value = "新增品牌")
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand,@RequestParam("categories") List<Long> cids){
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 品牌修改
     * @param brand
     * @param cids
     * @return
     */
    @ApiOperation(value = "修改品牌")
    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand,@RequestParam("categories") List<Long> cids){
        this.brandService.updateBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.ACCEPTED).build();
    }


    /**
     * 删除tb_brand中的数据，单个删除、多个删除二合一
     * @param bid
     * @return
     */
    @DeleteMapping("bid/{bid}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("bid") String bid){
        String separator="-";
        if(bid.contains(separator)){
            String[] ids=bid.split(separator);
            for(String id:ids){
                this.brandService.deleteBrand(Long.parseLong(id));
            }
        }else{
            this.brandService.deleteBrand(Long.parseLong(bid));
        }
        return ResponseEntity.status(HttpStatus.OK).build();
    }





//----------------------------------------


    /**
     * (GoodsFrom)根据cid查询品牌
     * @param cid
     * @return
     */
    @ApiOperation(value = "根据cid查询品牌")
    @GetMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid){
        return ResponseEntity.ok(brandService.queryBrandByCid(cid));
    }



    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询品牌")
    @GetMapping("{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(brandService.queryById(id));
    }

    @GetMapping("brandsList")
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(brandService.queryByIds(ids));
    }


    /**
     * 删除tb_category_brand中的数据
     * @param bid
     * @return
     */
    @DeleteMapping("cid_bid/{bid}")
    public ResponseEntity<Void> deleteByBrandIdInCategoryBrand(@PathVariable("bid") Long bid){
        brandService.deleteByBrandIdInCategoryBrand(bid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @GetMapping("/countBrands")
    public int queryBrands(){
        return brandService.countBrands();
    }
}
